@echo off
@chcp 65001
SETLOCAL ENABLEDELAYEDEXPANSION
MODE CON COLS=80 LINES=30
call :setESC

set ANDROID_ADB_SERVER_PORT=5037
set AGENT_VERSION=v5.0.1
set FILE_NAME=sunmi-dkp-agent-%AGENT_VERSION%-windows-x86.jar
set SFTP_HOST=10.14.3.136
set SFTP_USER=testtool01
set SFTP_PASS=sunmi388
set REMOTE_FILE=/media/testtool01/8f6e1601-d8df-444f-9604-e1a2b2726aa1/DKPAttachment/AgentFolder/%FILE_NAME%

echo.
echo %ESC%[32m清理历史数据%ESC%[0m

if exist CompatibilityTest (
    rmdir /s /q CompatibilityTest
)
mkdir CompatibilityTest
if exist gitTest (
    rmdir /s /q gitTest
)
mkdir gitTest
if exist test-output (
    rmdir /s /q test-output
)
mkdir test-output
del *.apk


echo.
echo %ESC%[32m清除旧版本%ESC%[0m
for %%i in (*-windows-x86.jar) do (
    if /I not "%%~nxi"=="%FILE_NAME%" (
        del "%%i"
		echo %%i
    )
)

if exist %FILE_NAME% goto RUNJAR


echo.
echo %ESC%[32m下载最新客户端%ESC%[0m
echo %FILE_NAME%

echo get %REMOTE_FILE% %FILE_NAME% > sftp_commands.txt
echo bye >> sftp_commands.txt
plugins\psftp %SFTP_USER%@%SFTP_HOST% -pw %SFTP_PASS% -b sftp_commands.txt
del sftp_commands.txt

echo.
echo %ESC%[32m下载成功%ESC%[0m


:RUNJAR

echo.
echo %ESC%[32m启动Agent客户端%ESC%[0m
echo 客户端版本:%AGENT_VERSION%
echo.
echo.
java -jar %FILE_NAME%
pause
exit

:setESC
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set ESC=%%b
exit /B 0
)
